from django.contrib import admin
from django.utils.html import format_html
from app.models import College, Student, Teacher, Class, Course, Grade, Major, Classroom


class CollegeAdmin(admin.ModelAdmin):
    list_display = ('id', 'college_name')
    list_filter = ('college_name',)
    ordering = ('id',)


class StudentAdmin(admin.ModelAdmin):
    # form = StudentForm
    list_display = ('display_image','student_id', 'major_id', 'class_id', 'name', 'age', 'gender', 'address')
    list_filter = ('class_id', 'major_id')
    search_fields = ('student_id', 'gender', 'name')
    ordering = ('student_id',)

    @admin.display(description='头像')
    def display_image(self, obj):
        if obj.img:
            return format_html('<img src="{}" width="50" height="50" />', obj.img.url)
        return None
    # def get_queryset(self, request):
    #     qs = super().get_queryset(request)
    #     # 获取当前专业
    #     major = request.GET.get('major_id', None)
    #     if major:
    #         # 根据专业过滤班级
    #         qs = qs.filter(major_id=major)
    #     return qs


class TeacherAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'major')
    list_filter = ('major',)


class ClassAdmin(admin.ModelAdmin):
    list_display = ('id', 'class_name', 'head_teacher')


class MajorAdmin(admin.ModelAdmin):
    list_display = ('id', 'major_name', 'college')
    list_filter = ('college',)
    ordering = ('id',)


class ClassroomAdmin(admin.ModelAdmin):
    list_display = ('id', 'location', 'capacity')


class CourseAdmin(admin.ModelAdmin):
    list_display = ('course_id', 'course_name', 'credits')


class GradeAdmin(admin.ModelAdmin):
    list_display = ('grade_id', 'student', 'course', 'score')
    list_filter = ('student', 'course')
    search_fields = ('grade_id', 'student__name', 'course__course_name')
    ordering = ('grade_id',)


# Register your models here.
admin.site.register(College, CollegeAdmin)
admin.site.register(Student, StudentAdmin)
admin.site.register(Teacher, TeacherAdmin)
admin.site.register(Class, ClassAdmin)
admin.site.register(Course, CourseAdmin)
admin.site.register(Grade, GradeAdmin)
admin.site.register(Major, MajorAdmin)
admin.site.register(Classroom, ClassroomAdmin)
